﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace SV
{
    
    class Program
    {
        public static string BASE = ConfigurationSettings.AppSettings["rootfolder"];
        public static string HOST=ConfigurationSettings.AppSettings["host"];
        
        static void Main(string[] args)
        {
            int PORT = int.Parse(ConfigurationSettings.AppSettings["port"]);
           int MAX_INPUT_ARRAY = int.Parse(ConfigurationSettings.AppSettings["max_input_array"]);
            Files file = new Files();
            Server sv = new Server(HOST, PORT);
            Func func=new Func();

        }

    }
    class Files
    {        
        Func func = new Func();
        public string readFile_outText(string file)
        {
            try
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    String line = sr.ReadToEnd();
                    return func.outRand(line.Split(';'));                                  
                }
            }
            catch (Exception e)
            {             
                return "%no" ;
            }
        }
        public string readFile(string file)
        {
            try
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    String line = sr.ReadToEnd();
                    return line;
                }
            }
            catch (Exception e)
            {
                return "%no";
            }
        }
        public void create(string textin, string textout)
        {
            string filename = func.md5(textin);
            string fileNamedir = @Program.BASE + filename;
            if (File.Exists(fileNamedir))
            {
                File.Delete(fileNamedir);
            }

            using (FileStream fs = File.Create(fileNamedir))
            {
                // Add some text to file
                Byte[] title = new UTF8Encoding(true).GetBytes(textout);
                fs.Write(title, 0, title.Length);
                fs.Flush();
                fs.Close();
            }
           
        }
        
    }

    class Client
    {
        TcpClient clientSocket;
        string clNo;
        public void startClient(TcpClient inClientSocket, string clineNo)
        {
            this.clientSocket = inClientSocket;
            this.clNo = clineNo;
            Thread ctThread = new Thread(doChat);
            ctThread.Start();
        }
        private void doChat()
        {
            int requestCount = 0;
            byte[] bytesFrom = new byte[999999];
            string dataFromClient = null;
            Byte[] sendBytes = null;
            string serverResponse = null;
            string rCount = null;
            requestCount = 0;
            Files file = new Files();
            Func func = new Func();
            while ((true))
            {
                try
                {
                    requestCount = requestCount + 1;
                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    dataFromClient = System.Text.Encoding.UTF8.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    //Xu ly
                    Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);

                    string filename = Program.BASE + func.md5(dataFromClient);
                    string outtext = file.readFile(filename);
                    if (outtext.CompareTo("%no") == 0)
                    {
                        outtext = "Hello";
                        file.create(dataFromClient,"hello");
                    }                   
                    //Tra ve
                    serverResponse = outtext;                    
                    sendBytes = Encoding.UTF8.GetBytes(serverResponse);
                    networkStream.Write(sendBytes, 0, sendBytes.Length);
                    networkStream.Flush();
                    networkStream.Close();
                    Console.WriteLine(" >> " + serverResponse);
                                    }
                catch (Exception ex)
                {
                    Console.WriteLine(" >> " + ex.ToString());
                }
            }
        }
    }
}
